﻿using System.Linq;

namespace Creventive.Wpf.ShellFactory.Utility
{
    /// <summary>
    ///   Provides extensions to the string class
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        ///   Converts the object to a string safely - if the object is null, <paramref name = "nullValue" /> is returned instead.
        /// </summary>
        /// <param name = "element">The element.</param>
        /// <param name = "nullValue">The null value.</param>
        /// <returns></returns>
        public static string ToStringSafe(this object element, string nullValue = null)
        {
            if (element == null)
                return nullValue;
            else
                return element.ToString();
        }

        /// <summary>
        ///   Trims the string safely - if the string is null, <paramref name = "nullValue" /> is returned instead.
        /// </summary>
        /// <param name = "input">The input.</param>
        /// <param name = "trimChars">The trim chars.</param>
        /// <param name = "nullValue">The null value.</param>
        /// <returns></returns>
        public static string TrimSafe(this string input, char[] trimChars, string nullValue = "")
        {
            if (input == null)
                return nullValue;
            else
                return input.Trim(trimChars);
        }

        /// <summary>
        ///   Trims the string safely - if the string is null, <paramref name = "nullValue" /> is returned instead.
        /// </summary>
        /// <param name = "input">The input.</param>
        /// <param name = "nullValue">The null value.</param>
        /// <returns></returns>
        public static string TrimSafe(this string input, string nullValue = "")
        {
            if (input == null)
                return nullValue;
            else
                return input.Trim();
        }
    }
}